package cn.kgc.springcloud.controller;

import cn.kgc.springcloud.client.HystrixClient;
import cn.kgc.springcloud.client.RoleClient;
import cn.kgc.springcloud.entity.Role;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/10/16
 * @description 如何通过openfeign实现服务间的通信
 * 1. 导包
 * 2. 配置参数
 * 3. 添加注解
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private RoleClient roleClient;

    @Resource
    private HystrixClient hystrixClient;

    @GetMapping("m1")
    public String method1() {
        String s = roleClient.method1();
        return " userController method1  ok --role-> " + s;
    }

    @GetMapping("m2")
    public String method2(Integer id) {
        String s = roleClient.method2(id);
        return " userController method2  ok --role-> " + s;
    }

    @GetMapping("m3/{id}")
    public String method3(@PathVariable("id") Integer id) {
        String s = roleClient.method3(id);
        return " userController method3  ok --role-> " + s;
    }

    @GetMapping("m4")
    public String method4() {

        Role role = new Role();
        role.setRoleName("root");
        role.setId(1);
        role.setDescription("这是一个管理员");

        String s = roleClient.method4(role.getId(), role.getRoleName(), role.getDescription());
        return " userController method4  ok --role-> " + s;
    }

    @GetMapping("m5")
    public String method5() {


        Role role = new Role();
        role.setRoleName("root");
        role.setId(1);
        role.setDescription("这是一个管理员");

        String s = roleClient.method5(role);
        return " userController method5  ok --role-> " + s;
    }


    @GetMapping("m6")
    public String method6() {

        String[] ids = {"1", "2", "3", "4"};

        String s = roleClient.method6(ids);
        return " userController method6  ok --role-> " + s;
    }


    @GetMapping("m7")
    public String method7() {

        // String s = roleClient.method7();
        // Role bean = JSONUtil.toBean(s, Role.class);
        // System.out.println("bean = " + bean.getRoleName());

        //  可以实现数据的隐式转换
        Role role = roleClient.method7();
        return " userController method7  ok --role-> " + role.getRoleName();
    }

    @GetMapping("m8")
    public String method8() {
        List<Role> roles = roleClient.method8();
        roles.forEach(System.out::println);
        return " userController method7  ok --role-> " ;
    }


    @GetMapping("m9")
    public String method9(Integer id) {
        String s = hystrixClient.method1(id);
        return " userController method9  ok --hystrix-> "+s ;
    }

}
